//
//  CardInputManager.m
//  AwesomeProject
//
//  Created by 李顺风 on 2021/3/25.
//

#import "CardInputManager.h"
#import <React/RCTFont.h>

@implementation CardInputManager


RCT_EXPORT_MODULE(CardInput)
RCT_EXPORT_VIEW_PROPERTY(onChange, RCTBubblingEventBlock)

- (UIView *)view
{

  CardInputView *view = [CardInputView new];
  view.resultBlock = ^(STPPaymentCardTextField * _Nonnull textField, CardInputView * _Nonnull view) {
    if (view.onChange) {
      NSLog(@"result.number===>%@",textField.cardParams.number);
      view.onChange(@{@"number":textField.cardParams.number,
                      @"expYear":textField.cardParams.expYear == nil ? 0 :textField.cardParams.expYear,
                      @"expMonth":textField.cardParams.expMonth == nil ? 0 :textField.cardParams.expMonth,
                      @"cvc":textField.cardParams.cvc == nil ? @"":textField.cardParams.cvc,
                      @"isValid":@(textField.isValid),
                    });
    }
  };
  return view;
}


RCT_EXPORT_VIEW_PROPERTY(textColor, UIColor);
RCT_EXPORT_VIEW_PROPERTY(borderColor, UIColor);
RCT_EXPORT_VIEW_PROPERTY(borderWidth, CGFloat);
RCT_EXPORT_VIEW_PROPERTY(cornerRadius, CGFloat);
RCT_EXPORT_VIEW_PROPERTY(cursorColor, UIColor);
RCT_EXPORT_VIEW_PROPERTY(textErrorColor, UIColor);
RCT_EXPORT_VIEW_PROPERTY(placeholderColor, UIColor);

RCT_CUSTOM_VIEW_PROPERTY(fontSize, NSNumber, CardInputView)
{
    view.font = [RCTFont updateFont:view.font withSize:json ?: @(defaultView.font.pointSize)];
}
RCT_CUSTOM_VIEW_PROPERTY(fontWeight, NSString, __unused CardInputView)
{
    view.font = [RCTFont updateFont:view.font withWeight:json]; // defaults to normal
}
RCT_CUSTOM_VIEW_PROPERTY(fontStyle, NSString, __unused CardInputView)
{
    view.font = [RCTFont updateFont:view.font withStyle:json]; // defaults to normal
}
RCT_CUSTOM_VIEW_PROPERTY(fontFamily, NSString, CardInputView)
{
    view.font = [RCTFont updateFont:view.font withFamily:json ?: defaultView.font.familyName];
}

RCT_EXPORT_VIEW_PROPERTY(numberPlaceholder, NSString);
RCT_EXPORT_VIEW_PROPERTY(expirationPlaceholder, NSString);
RCT_EXPORT_VIEW_PROPERTY(cvcPlaceholder, NSString);

RCT_EXPORT_VIEW_PROPERTY(enabled, BOOL);



RCT_CUSTOM_VIEW_PROPERTY(params, NSDictionary, CardInputView)
{
    STPCardParams *cardParams = [[STPCardParams alloc] init];

    [cardParams setValuesForKeysWithDictionary:json];

    [view setCardParams:cardParams];
}

RCT_CUSTOM_VIEW_PROPERTY(keyboardAppearance, NSString, __unused CardInputView)
{
    [view setKeyboardAppearance:[RCTConvert UIKeyboardAppearance:json]];
}


@end
